package a.b.reggie.service.impl;

import a.b.reggie.dto.OrdersDto;
import a.b.reggie.mapper.OrderMapper;
import a.b.reggie.pojo.*;
import a.b.reggie.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class IOrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements IOrderService {

    @Autowired
    private IUserService userService;

    @Autowired
    private IShoppingCartService shoppingCartService;

    @Autowired
    private IOrderDetailService orderDetailService;

    @Autowired
    private IAddressBookService addressBookService;

    /**
     * 提交订单
     * @param phone
     * @param ordersDto
     */
    @Transactional
    public void submit(Long phone, OrdersDto ordersDto) {
        //根据phone查询用户id
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,phone);
        User user = userService.getOne(queryWrapper);
        Long userId = user.getId();

        //根据用户id查询购物车中菜品信息
        LambdaQueryWrapper<ShoppingCart> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper1);
        //计算订单总金额
        int amount = 0;
        for (ShoppingCart shoppingCart:list){
            amount+=shoppingCart.getNumber()*shoppingCart.getAmount().intValue();
        }

        //查询地址信息
        Long addressBookId = ordersDto.getAddressBookId();
        AddressBook byId = addressBookService.getById(addressBookId);
        ordersDto.setUserName(byId.getConsignee());
        ordersDto.setPhone(byId.getPhone());
        ordersDto.setAddress(byId.getDetail());

        //设置订单信息
        ordersDto.setUserId(userId);
        ordersDto.setOrderTime(LocalDateTime.now());
        ordersDto.setCheckoutTime(LocalDateTime.now());
        ordersDto.setAmount(BigDecimal.valueOf(amount));
        double number = Math.random() * 100000;
        ordersDto.setNumber(String.valueOf((int)number));
        ordersDto.setStatus(2);


        this.save(ordersDto);

        //查询订单id
        LambdaQueryWrapper<Orders> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Orders::getUserId,userId);
        //按时间排序获取最新订单
        queryWrapper2.orderByDesc(Orders::getOrderTime);
        Page<Orders> page = new Page(1,1);
        this.page(page, queryWrapper2);
        List<Orders> records = page.getRecords();
        Long orderId = records.get(0).getId();
        log.info("orderId"+orderId);

        //将购物车中的菜品信息存入orderDetail
        for (ShoppingCart shoppingCart:list){
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            BeanUtils.copyProperties(shoppingCart,orderDetail);
            orderDetailService.save(orderDetail);
        }

        //清空购物车
        shoppingCartService.remove(null);

    }

    /**
     * 用户获取订单分页信息
     * @param page
     * @param pageSize
     * @return
     */
    public Page Upage(Object phone,int page, int pageSize) {

        //根据phone查询用户id
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,phone);
        User user = userService.getOne(queryWrapper);
        Long userId = user.getId();

        //设置分页条件
        Page<Orders> page1 = new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Orders> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Orders::getUserId,userId);
        queryWrapper1.orderByDesc(Orders::getOrderTime);
        this.page(page1,queryWrapper1);


        //复制分页基本信息
        BeanUtils.copyProperties(dtoPage,page1,"records");

        List<Orders> records = page1.getRecords();
        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders order:records){
            OrdersDto ordersDto = new OrdersDto();
            //根据订单id查询菜品集合信息
            Long ordersId = order.getId();
            LambdaQueryWrapper<OrderDetail> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(OrderDetail::getOrderId,ordersId);
            List<OrderDetail> list = orderDetailService.list(queryWrapper2);

            //将订单信息和菜品集合信息复制到OrderDto中
            BeanUtils.copyProperties(order,ordersDto);
            ordersDto.setOrderDetails(list);

            ordersDtoList.add(ordersDto);

        }

        //设置dtoPage的records
        dtoPage.setRecords(ordersDtoList);

        return dtoPage;
    }

    /**
     * 派送订单
     * @param map
     */
    public void updateStatus(Map map) {
        Long id = Long.valueOf(String.valueOf(map.get("id")));
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId,id);
        Orders one = this.getOne(queryWrapper);
        one.setStatus(3);
        this.updateById(one);
    }

    /**
     * 商家获取订单信息
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    public Page Spage(int page, int pageSize, Long number,String beginTime,String endTime) {

        //设置查询条件
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Orders::getOrderTime);

        queryWrapper.eq(number!=null,Orders::getNumber,number);
        queryWrapper.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        queryWrapper.le(endTime!=null,Orders::getOrderTime,endTime);
        //设置分页条件
        Page<Orders> page1 = new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage = new Page<>();

        //获取分页信息
        this.page(page1,queryWrapper);

        //复制分页基本信息
        BeanUtils.copyProperties(dtoPage,page1,"records");

        List<Orders> records = page1.getRecords();
        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders order:records){
            OrdersDto ordersDto = new OrdersDto();
            //根据订单id查询菜品集合信息
            Long ordersId = order.getId();
            LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId,ordersId);
            List<OrderDetail> list = orderDetailService.list(queryWrapper1);

            //将订单信息和菜品集合信息复制到OrderDto中
            BeanUtils.copyProperties(order,ordersDto);
            ordersDto.setOrderDetails(list);

            ordersDtoList.add(ordersDto);

        }

        //设置dtoPage的records
        dtoPage.setRecords(ordersDtoList);


        return dtoPage;
    }
}
